
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

namespace Articles.Gui.AspNet
{
	public class FileAttachmentGroupsListDisplay : Syrinx.Gui.AspNet.Ajax.ComplexBizObjListDisplay
	{
		public FileAttachmentGroupsListDisplay()
			:base()
		{
		}

		public FileAttachmentGroupsListDisplay(ActiveWidget widget)
			:base(widget)
		{
		}

		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			m_reallyRenderContents = HasFileAttachments;
			base.Render(writer);
		}

		public override void getState(System.Xml.XmlElement clientState, string pageHtml)
		{
			base.getState(clientState, pageHtml);
			if (!HasFileAttachments)
				clientState.SetAttribute("html", "<div></div>");
		}

		protected bool HasFileAttachments
		{
			get
			{
				return (m_bizObjCursor != null && m_bizObjCursor.numElements > 1);
			}
		}

		public bool IsAuthorizedForFile
		{
			get
			{
				if (CurrentBizObj.isKindOf("ArticleFileAttachment"))
					return (bool)CurrentBizObj.get("authorized");
				return true;
			}
		}

		public void processFileList(HtmlBlockInfo info)
		{
			if (m_bizObjCursor == null)
				return;
			IBizObj fileGroup = m_bizObjCursor.currentElement;
			if (fileGroup == null)
				return;
			int p = 0;
			foreach (IBizObj file in (IBizObjList)fileGroup.get("files"))
			{
				CurrentBizObj = file;
				string key = generateClientObjectKey(file, null)+"_"+ p++;

				IList list = info.generateBlock(key);

				foreach (string[] name in list)
					processField(name[1], name[0]);
			}
			CurrentBizObj = m_bizObjCursor.currentElement;
		}
	}
}
